package com.demo.controller;


import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.*;


/**
 * @version v1.0
 * @author 梁阿龙
 * @date 2023-02-06 11:44*/
@RestController
@RequestMapping("/url")
public class urlController {




    public static void main(String[] args) {
        String url = "http://www.baidu.com/";
        if (java.awt.Desktop.isDesktopSupported()) {
            try {
                // 创建一个URI实例
                java.net.URI uri = java.net.URI.create(url);
                // 获取当前系统桌面扩展
                java.awt.Desktop dp = java.awt.Desktop.getDesktop();
                // 判断系统桌面是否支持要执行的功能
                if (dp.isSupported(java.awt.Desktop.Action.BROWSE)) {
                    // 获取系统默认浏览器打开链接
                    dp.browse(uri);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/dapingone")
    public void dapingone(){
        System.setProperty("java.awt.headless", "false");
        System.err.println("开始调用");
        try {
            String url = "http://xxtb.e-heating.cn/";
            //String url = "https://www.jb51.net/";
            java.net.URI uri = java.net.URI.create(url);
            // 获取当前系统桌面扩展
            java.awt.Desktop dp = java.awt.Desktop.getDesktop();
            // 判断系统桌面是否支持要执行的功能
            if (dp.isSupported(java.awt.Desktop.Action.BROWSE)) {
                //File file = new File("D:\\aa.txt");
                //dp.edit(file);// 　编辑文件
                dp.browse(uri);// 获取系统默认浏览器打开链接
                // dp.open(file);// 用默认方式打开文件
                // dp.print(file);// 用打印机打印文件
            }
        } catch (java.lang.NullPointerException e) {
            // 此为uri为空时抛出异常
            e.printStackTrace();
        } catch (java.io.IOException e) {
            // 此为无法获取系统默认浏览器
            e.printStackTrace();
        }
    }



    @RequestMapping("/daping3")
    public void daping3(){
        System.setProperty("java.awt.headless", "false");
        System.err.println("开始调用");
        try {
            Desktop.getDesktop().browse(new URL("http://www.baidu.com").toURI());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("/dapingtwo")
    public void dapingtwo()  throws IOException{
        try {
            URL url = new URL("http://www.baidu.com");
            URLConnection URLconnection = url.openConnection();
            HttpURLConnection httpConnection = (HttpURLConnection) URLconnection;
            int responseCode = httpConnection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                System.err.println("成功");
                InputStream in = httpConnection.getInputStream();
                InputStreamReader isr = new InputStreamReader(in);
                BufferedReader bufr = new BufferedReader(isr);
                String str;
                while ((str = bufr.readLine()) != null) {
                    System.out.println(str);
                }
                bufr.close();
            } else {
                System.err.println("失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @RequestMapping("/daping4")
    public ModelAndView daping4() throws Exception {
//        System.setProperty("java.awt.headless", "false");
        System.err.println("访问:http://xxtb.e-heating.cn/webroot/decision/view/form?viewlet=Screen%252Fqianyi_auto.frm&op=form_adaptive");
//        String contNo =req.getParameter("contNo");
        String url = "redirect:http://xxtb.e-heating.cn/webroot/decision/view/form?viewlet=Screen%252Fqianyi_auto.frm&op=form_adaptive";
        return new ModelAndView(url);
    }

    @RequestMapping("alipayforward1")
    public String alipayforward1(@RequestParam("contNo") String contNo, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //String contNo =req.getParameter("contNo"); //保单号
        System.err.println("访问/downloadRequestElecCont.action");
        return "redirect:http://baidu.com/downloadRequestElecCont.action?contNo="+contNo;
    }


    @RequestMapping("alipayforward2/{contNo}")
    public void alipayforward2(@PathVariable("contNo") String contNo, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //String contNo =req.getParameter("contNo"); //保单号
        System.err.println("访问/downloadRequestElecCont.action");
        resp.sendRedirect("http://baidu.com/downloadRequestElecCont.action?contNo="+contNo);
    }



}
